# 模型评估函数
def evaluate_model(X_test, y_test, best_weights, classifiers):
    predictions = np.sign(np.sum(
        [best_weights[j] * weak_classifier(X_test, classifiers[j][0], classifiers[j][1]) 
         for j in range(len(classifiers))], axis=0))
    accuracy = np.mean(predictions == y_test)
    return accuracy

# 评估模型性能
accuracy = evaluate_model(X_test, y_test, best_weights, classifiers)
print(f"模型的准确率为: {accuracy:.4f}")

# 计算精确率和召回率
def calculate_metrics(X_test, y_test, best_weights, classifiers):
    predictions = np.sign(np.sum(
        [best_weights[j] * weak_classifier(X_test, classifiers[j][0], classifiers[j][1]) 
         for j in range(len(classifiers))], axis=0))
    
    # 转换为0/1标签
    y_true = (y_test == 1).astype(int)
    y_pred = (predictions == 1).astype(int)
    
    # 计算混淆矩阵
    TP = np.sum((y_true == 1) & (y_pred == 1))
    FP = np.sum((y_true == 0) & (y_pred == 1))
    FN = np.sum((y_true == 1) & (y_pred == 0))
    
    precision = TP / (TP + FP) if (TP + FP) > 0 else 0
    recall = TP / (TP + FN) if (TP + FN) > 0 else 0
    
    return precision, recall

precision, recall = calculate_metrics(X_test, y_test, best_weights, classifiers)
print(f"精确率: {precision:.4f}, 召回率: {recall:.4f}")